Attribute VB_Name = "Mod_Detalle"
Option Explicit
Public wjCodigo As String
Dim Col As TrueOleDBGrid80.Column
Dim Cols As TrueOleDBGrid80.Columns
Public Enum enmControlTipo
    TDBGrid = 0
    ComboBox = 1
End Enum

Public Sub LimpiarFiltro(Ado As Adodc, Grilla As TDBGrid)
    For Each Col In Grilla.Columns
        Col.FilterText = ""
    Next Col
End Sub

Public Sub Buscar_TruedbGrilla(Dc As Adodc, Nombre As TDBGrid)
On Error GoTo ErrHandler
    Dim c As Integer
    With Nombre
        Set Cols = .Columns
        c = .Col
       .HoldFields
       Dc.Recordset.Filter = GetFilter()
       .Col = c
       .EditActive = True
     End With
     Exit Sub
ErrHandler:
    MsgBox Err.Source & ":" & vbCrLf & Err.Description
End Sub

Public Function GetFilter() As String
    Dim tmp As String
    Dim N As Integer
    For Each Col In Cols
        If Trim(Col.FilterText) <> "" Then
            N = N + 1
            If N > 1 Then
                tmp = tmp & " AND "
            End If
            tmp = tmp & Col.DataField & " LIKE '" & Col.FilterText & "*'"
        End If
    Next Col
    GetFilter = tmp
End Function

Public Sub Limpia_Controls(F As Form, Clase As String)
On Error GoTo Mensaje
    Dim Nc As Integer, i As Integer
    Nc = F.Controls.Count - 1
    For i = 0 To Nc
        If (TypeName(F.Controls(i)) = Clase) Then
            F.Controls(i).Text = ""
        End If
    Next i
    Exit Sub
Mensaje:
    MsgBox Err.Description, vbInformation + vbCritical
End Sub

Public Sub BackColores_Controls(F As Form, Clase As String, xtColor As String)
On Error GoTo Mensaje
    Dim Nc As Integer, i As Integer
    Nc = F.Controls.Count - 1
    For i = 0 To Nc
        If (TypeName(F.Controls(i)) = Clase) Then
            F.Controls(i).BackColor = xtColor
        End If
    Next i
    Exit Sub
Mensaje:
    MsgBox Err.Description, vbInformation + vbCritical
End Sub

Public Sub Habilitar_Controles(F As Form, Clase As String, Valor As Boolean)
On Error GoTo Mensaje
    Dim Nc As Integer, i As Integer
    Nc = F.Controls.Count - 1
    For i = 0 To Nc
        If (TypeName(F.Controls(i)) = Clase) Then
            F.Controls(i).Enabled = Valor
        End If
    Next i
    Exit Sub
Mensaje:
    MsgBox Err.Description, vbInformation + vbCritical
End Sub

Public Sub Carga_Linea(F As Form)
Dim Rs1 As New ADODB.Recordset
  Set Rs1 = CLS_COMANDO.Ejec_Comando_1("sp_Inv_Trae_Linea", gbCodEmpresa$, "In01CodLinArt", "*")
  If Rs1.RecordCount > 0 Then
        Rs1.MoveFirst
        F.CboLinea.Clear
        While Not Rs1.EOF
           F.CboLinea.AddItem Trim$(Rs1("In01CodLinArt")) + " - " + Trim$(Rs1("In01Descripcion"))
           Rs1.MoveNext
        Wend
        Set Rs1 = Nothing
  End If
End Sub

Public Sub Carga_Clase(F As Form, WcLinea)
On Error GoTo Mio
    Dim Rs1 As New ADODB.Recordset
    Set Rs1 = CLS_COMANDO.Ejec_Comando_1("Spu_Inv_Trae_SubLinea", gbCodEmpresa$, "In02CodLinArt", WcLinea)
    If Rs1.RecordCount > 0 Then
        Rs1.MoveFirst
        F.CboSubLinea.Clear
        While Not Rs1.EOF
             F.CboSubLinea.AddItem Trim$(Rs1("In02CodGrupArt")) + " - " + Trim$(Rs1("In02Descripcion"))
             Rs1.MoveNext
         Wend
         Set Rs1 = Nothing
    End If
    Exit Sub
Mio:
    MsgBox Err.Description
    Exit Sub
End Sub

Public Sub Carga_Familia(F As Form, WcLinea, WcClase)
On Error GoTo Mio
    Dim Rs1 As New ADODB.Recordset
    Set Rs1 = CLS_COMANDO.Ejec_Comando_1("Spu_Inv_Trae_Familia", gbCodEmpresa$, WcLinea, WcClase, "In03CodGrupArt", WcClase)
    If Rs1.RecordCount > 0 Then
        Rs1.MoveFirst
        F.CboFamilia.Clear
        While Not Rs1.EOF
             F.CboFamilia.AddItem Trim$(Rs1("in03codfamilia")) + " - " + Trim$(Rs1("in03descripcion"))
             Rs1.MoveNext
         Wend
         Set Rs1 = Nothing
    End If
    Exit Sub
Mio:
    MsgBox Err.Description
    Exit Sub
End Sub

Public Sub Carga_SubFamilia(F As Form, WcLinea, WcClase, WcFamilia)
On Error GoTo Mio
    Dim Rs1 As New ADODB.Recordset
    Set Rs1 = CLS_COMANDO.Ejec_Comando_1("Spu_Inv_Trae_SubFamilia", gbCodEmpresa$, WcLinea, WcClase, WcFamilia, "In04CodFamilia", WcFamilia)
    If Rs1.RecordCount > 0 Then
        Rs1.MoveFirst
        F.CboSubFamilia.Clear
        While Not Rs1.EOF
             F.CboSubFamilia.AddItem Trim$(Rs1("in04codsubfamilia")) + " - " + Trim$(Rs1("in04descripcion"))
             Rs1.MoveNext
         Wend
         Set Rs1 = Nothing
    End If
    Exit Sub
Mio:
    MsgBox Err.Description
    Exit Sub
End Sub

Public Sub BuscaLinea(F As Form, WLinea)
Dim i As Integer
   For i = 0 To F.CboLinea.ListCount - 1
      If Left(F.CboLinea.List(i), 1) = Left(WLinea, 1) Then
          F.CboLinea.ListIndex = (i)
          Exit For
      End If
   Next
End Sub

Public Sub BuscaSubLinea(F As Form, WSubLinea)
Dim i As Integer
   For i = 0 To F.CboSubLinea.ListCount - 1
      If Left(F.CboSubLinea.List(i), 2) = Left(WSubLinea, 2) Then
          F.CboSubLinea.ListIndex = (i)
          Exit For
      End If
   Next
End Sub

Public Sub BuscaFamilia(F As Form, WFamilia)
Dim i As Integer
   For i = 0 To F.CboFamilia.ListCount - 1
      If Left(F.CboFamilia.List(i), 2) = Left(WFamilia, 2) Then
          F.CboFamilia.ListIndex = (i)
          Exit For
      End If
   Next
End Sub

Public Sub BuscaSubFamilia(F As Form, WSubFamilia)
Dim i As Integer
   For i = 0 To F.CboSubFamilia.ListCount - 1
      If Left(F.CboSubFamilia.List(i), 2) = Left(WSubFamilia, 2) Then
          F.CboSubFamilia.ListIndex = (i)
          Exit For
      End If
   Next
End Sub

Public Sub Crear_TablaDetalle()
On Error GoTo Mensaje
    Set Ry_Detalle = New ADODB.Recordset
    With Ry_Detalle.Fields
        .Append "Articulo", adVarChar, 20
        .Append "Descripcion", adVarChar, 200
        .Append "Unidad", adVarChar, 100
        .Append "Cantidad", adDouble
        .Append "Almacen", adVarChar, 2
        .Append "Glosa", adVarChar, 300
    End With
    Ry_Detalle.Open
    Exit Sub
Mensaje:
    MsgBox Err.Description
End Sub

Public Sub Inserta_TablaDetalle(ByVal Campo0 As String, ByVal Campo1 As String, ByVal Campo2 As String, ByVal Campo3 As Double, ByVal Campo4 As String)
On Error GoTo Mio
    With Ry_Detalle
        .AddNew
        .Fields(0) = Campo0
        .Fields(1) = Campo1
        .Fields(2) = Campo2
        .Fields(3) = Campo3
        .Fields(4) = Campo4
        .Update
    End With
    Exit Sub
Mio:
    MsgBox Err.Description, vbInformation + vbCritical
End Sub

Public Sub Hablilita_Grilla(F As Form, xcol As Integer, Valor As Boolean)
On Error GoTo Mio
    Dim i As Integer
    For i = 0 To xcol
        F.tblDetalle.Columns(i).Locked = Valor
    Next i
    Exit Sub
Mio:
   MsgBox Err.Description
End Sub

Public Sub Ftab(intTecla As Integer)
    'EH 200305
    On Error GoTo ControlError
        If intTecla = 13 Or intTecla = 40 Then
            SendKeys ("{tab}")
            Exit Sub
        End If
        If intTecla = 38 Then SendKeys ("+{tab}")
    Exit Sub
ControlError:
End Sub

Public Function DameDescripcionRomantex(cCodE, cCodigoBus, cFlag$) As String
    Set Cmd = CLS_COMANDO.Ejec_Comando_2("sp_Fac_Dame_Descripcion_Romantex", cCodE, cCodigoBus, cFlag$, "")
    DameDescripcionRomantex = aTexto(Cmd(4))
End Function

Public Sub SeleccionaItemProduccion()
On Error GoTo Mio
    Dim RS As New ADODB.Recordset, cTranMovi$, nCont%
    cTranMovi$ = DameDescripcion(gbCodEmpresa$ + fAbcDocumento.txtTransaccion, "TRANMOVI")
    Set RS = CLS_COMANDO.Ejec_Comando_1("Spu_Inv_TraeDetOrdenesP", gbCodEmpresa, gbAno, Trim$(fAbcDocumento.txtPedidos), cTranMovi$)
    
    If fAbcDocumento.InvMos_Ins = "S" And cTranMovi$ = "S" Then
        Set RS = CLS_COMANDO.Ejec_Comando_1("sp_Inv_Trae_Composicion_Kit", gbCodEmpresa$, gbAno, Trim$(RS("In41Key")))
    ElseIf fAbcDocumento.InvMos_Ins = "S" And cTranMovi$ = "E" Then
        Set RS = CLS_COMANDO.Ejec_Comando_1("Spu_Inv_Traer_ConsumoOrden_New", gbCodEmpresa$, gbAno, Trim$(fAbcDocumento.txtTipoDocu), Trim$(fAbcDocumento.txtTransaccion), Trim(fAbcDocumento.txtPedidos), Trim$(fAbcDocumento.LblTipo))
    End If
    
    If RS.RecordCount > 0 Then
        Call Crear_DetalleDocumento: RS.MoveFirst
        For nCont% = 0 To RS.RecordCount - 1
            If fAbcDocumento.InvMos_Ins = "S" And cTranMovi$ = "S" Then
                    Call Llenar_GrillaDocumento(Trim$(RS("IN01KEY")), Trim$(RS("IN01DESLAR")), Trim$(RS("In01UniMed")), RS("In01Cant"), RS("In01CosUniSol"), RS("In01CosUniDol"), Trim$("01"), "", cTranMovi$, gbAno)
            ElseIf fAbcDocumento.InvMos_Ins = "S" And cTranMovi$ = "E" Then
                    Call Llenar_GrillaDocumento(Trim$(RS("IN07KEY")), Trim$(RS("IN01DESLAR")), Trim$(RS("IN07UNIMED")), RS("IN07CANART"), 0, 0, Trim$(RS("IN07CODALM")), "", cTranMovi$, Trim$(RS("ANIO")))
            Else
                Call Llenar_GrillaDocumento(Trim$(RS("In41Key")), Trim$(RS("In41Deslar")), Trim$(RS("In41Unid")), RS("In41Cantidad"), RS("Precio"), RS("Importe"), Trim$("02"), "", cTranMovi$, Trim$(RS("In41Anio")))
            End If
            RS.MoveNext
            If Not RS.EOF Then Ry_Detalle.AddNew
        Next nCont%
    End If
    Exit Sub
Mio:
    MsgBox Err.Description, vbInformation + vbCritical
End Sub

Public Sub SeleccionaItemRequerimientoPedido()
On Error GoTo Mio
    Dim RS As New ADODB.Recordset, cTranMovi$, nCont%
    cTranMovi$ = DameDescripcion(gbCodEmpresa$ + fAbcDocumento.txtTransaccion, "TRANMOVI")
    Set RS = CLS_COMANDO.Ejec_Comando_1("Usp_Inv_TraeDetReqPedido", gbCodEmpresa, gbAno, Trim$(fAbcDocumento.txtCenCosto), Trim$(fAbcDocumento.txtPedido), cTranMovi$)
    
    If RS.RecordCount > 0 Then
        Call Crear_DetalleDocumento: RS.MoveFirst
        For nCont% = 0 To RS.RecordCount - 1
            If fAbcDocumento.InvMos_Ins = "S" And cTranMovi$ = "S" Then
                    Call Llenar_GrillaDocumento(Trim$(RS("IN60CODART")), Trim$(RS("IN60DESART")), Trim$(RS("IN60UNIDAD")), RS("IN60CANTIDAD"), RS("in60CosUniSol"), RS("in60CosUniDol"), Trim$(RS("IN60CODALM")), "", cTranMovi$, gbAno)
            ElseIf fAbcDocumento.InvMos_Ins = "S" And cTranMovi$ = "E" Then
                    Call Llenar_GrillaDocumento(Trim$(RS("IN60CODART")), Trim$(RS("IN60DESART")), Trim$(RS("IN60UNIDAD")), RS("IN60CANTIDAD"), RS("in60CosUniSol"), RS("in60CosUniDol"), Trim$(RS("IN60CODALM")), "", cTranMovi$, Trim$(RS("in60aa")))
            Else
                Call Llenar_GrillaDocumento(Trim$(RS("IN60CODART")), Trim$(RS("IN60DESART")), Trim$(RS("IN60UNIDAD")), RS("IN60CANTIDAD"), RS("in60CosUniSol"), RS("in60CosUniDol"), Trim$(RS("IN60CODALM")), "", cTranMovi$, Trim$(RS("in60aa")))
            End If
            RS.MoveNext
            If Not RS.EOF Then Ry_Detalle.AddNew
        Next nCont%
    End If
    Exit Sub
Mio:
    MsgBox Err.Description, vbInformation + vbCritical
End Sub

Public Sub ControlUbicar(ctlBase As Control, ctlHija As Control, Optional intTipoContol As enmControlTipo = TDBGrid, _
                         Optional intAling As Integer = 0)
On Error GoTo Falla
    Select Case Val(intTipoContol)
        Case TDBGrid
            ctlHija.Top = (ctlBase.Height * 2.5) + (ctlBase.Top)
            If intAling = 0 Then
                ctlHija.Left = ctlBase.Left + 20
            Else
                ctlHija.Left = (ctlBase.Left + ctlBase.Width) - (ctlHija.Width - 85)
            End If
        Case ComboBox
    End Select
    Exit Sub
Falla:
End Sub

Public Sub Siguiente(FlexGridListaPersona As VSFlexGrid)
   If (FlexGridListaPersona.Row < FlexGridListaPersona.Rows - 1) Then
       FlexGridListaPersona.Row = FlexGridListaPersona.Row + 1
       wjCodigo = Trim(FlexGridListaPersona.TextMatrix(FlexGridListaPersona.Row, 0))
   End If
End Sub

Public Sub Anterior(FlexGridListaPersona As VSFlexGrid)
   If (FlexGridListaPersona.Row > 1) Then
       FlexGridListaPersona.Row = FlexGridListaPersona.Row - 1
       wjCodigo = Trim(FlexGridListaPersona.TextMatrix(FlexGridListaPersona.Row, 1))
   End If
End Sub

Public Sub Ultimo(FlexGridListaPersona As VSFlexGrid)
   If (FlexGridListaPersona.Row < FlexGridListaPersona.Rows - 1) Then
        FlexGridListaPersona.Row = FlexGridListaPersona.Rows - 1
        wjCodigo = Trim(FlexGridListaPersona.TextMatrix(FlexGridListaPersona.Row, 1))
   End If
End Sub

Public Sub Pintar_Check_Flex(Flex As VSFlexGrid, ctFila As Integer, ctColum As Integer, ctDesde As Integer, ctHasta As Integer)
On Error GoTo ErrRango
  Dim i As Integer, ii, intFila, intFilaP, r
  With Flex
        If .Cell(flexcpChecked, ctFila, ctColum) = flexChecked Then
            intFilaP = ctDesde
            For ii = ctDesde To ctHasta
                .Col = intFilaP
                .CellBackColor = &HFFE1C4    '&H80FF&
                intFilaP = intFilaP + 1
            Next
        Else
            intFilaP = ctDesde
            For ii = ctDesde To ctHasta
                .Col = intFilaP
                .CellBackColor = vbWhite
                intFilaP = intFilaP + 1
            Next
        End If
  End With
  Exit Sub
ErrRango:
  MsgBox Err.Description, vbInformation
End Sub

Public Sub Tecla_Dow(NroTecla As String, F As Form)
Select Case NroTecla
    Case "114"
        F.cmdNew_Click
    Case "115"
        F.cmdDelete_Click
    Case "116"
        F.cmdModify_Click
End Select
End Sub

Public Function ConsistenciaNumeroDecimal(Num As Integer) As Integer
    Select Case Num
        Case Asc("0") To Asc("9"): ConsistenciaNumeroDecimal = Num
        Case Asc("."): ConsistenciaNumeroDecimal = Num
        Case 13, 27, 9, 8: ConsistenciaNumeroDecimal = Num
        Case Else: ConsistenciaNumeroDecimal = 0
    End Select
End Function

